package com.mes.cloud.controller;

import com.github.pagehelper.PageInfo;
import com.mes.cloud.dto.BcViewDTO;
import com.mes.cloud.service.BcViewService;
import com.mes.cloud.util.CommPager;
import com.mes.cloud.util.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 表字段
 *
 * @author wxm
 * @Time 2019-5-22
 */
@Api(tags = "BcViewController", description = "View配置")
@RestController
@RequestMapping("/bmeBcView")
public class BcViewController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BcViewController.class);

    @Autowired
    private BcViewService bcViewService;

    /**
     * @param map
     * @Description 获取列表
     * @date: 2019/6/27 14:41
     * @return: com.mes.cloud.util.JSONResult
     */
    @ApiOperation(value = "列表 无分页")
    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    public JSONResult findList(@RequestBody Map<Object, Object> map) {
        JSONResult jsonResult = null;
        try {
            List<BcViewDTO> bcViewList = bcViewService.getBcViewList(map);
            jsonResult = JSONResult.success(bcViewList);
        } catch (Exception e) {
            LOGGER.error("Class BcViewController:Method findList :inParam e: " + e.getMessage());
            jsonResult = JSONResult.fail(201, e.getMessage());
        }
        return jsonResult;
    }

    /**
     * @param map
     * @Description 获取分页列表
     * @date: 2019/6/27 14:42
     * @return: com.mes.cloud.util.JSONResult
     */
    @ApiOperation(value = "列表 分页")
    @RequestMapping(value = "/findPageList", method = RequestMethod.POST)
    public JSONResult findPageList(@RequestBody Map<Object, Object> map) {
        JSONResult jsonResult = null;
        try {
            CommPager<BcViewDTO> pageInfo = bcViewService.getBcViewListPage(map);
            jsonResult = JSONResult.success(pageInfo);
        } catch (Exception e) {
            LOGGER.error("Class BcViewController :Method findPageList:OutParam E: " + e.getMessage());
            jsonResult = JSONResult.fail(201, e.getMessage());
        }
        return jsonResult;
    }

    /**
     * @param map
     * @Description 获取记录总数
     * @date: 2019/6/27 14:42
     * @return: com.mes.cloud.util.JSONResult
     */
    @ApiOperation(value = "获取记录总数")
    @RequestMapping(value = "/getCount", method = RequestMethod.POST)
    public JSONResult getCount(@RequestBody Map<Object, Object> map) {
        JSONResult jsonResult = null;
        try {
            long count = bcViewService.getBcViewCount(map);
            jsonResult = JSONResult.success(count);
        } catch (Exception e) {
            LOGGER.error("Class BcViewController:Method getCount:OutParam E: " + e.getMessage());
            jsonResult = JSONResult.fail(201, e.getMessage());
        }
        return jsonResult;
    }

    /**
     * @param bcViewDTO
     * @Description 插入记录
     * @date: 2019/6/27 14:43
     * @return: com.mes.cloud.util.JSONResult
     */
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JSONResult insert(@RequestBody BcViewDTO bcViewDTO) {
        JSONResult jsonResult = null;
        try {
            long count = bcViewService.insertBcView(bcViewDTO);
            if (count > 0) {
                jsonResult = JSONResult.success();
            } else {
                jsonResult = JSONResult.fail(201, "操作失败");
            }
        } catch (Exception e) {
            LOGGER.error("Class BcViewController:Method insert:OutParam E: " + e.getMessage());
            jsonResult = JSONResult.fail(201, e.getMessage());
        }
        return jsonResult;
    }

    /**
     * @param bcViewDTO
     * @Description 更新记录
     * @date: 2019/6/27 14:43
     * @return: com.mes.cloud.util.JSONResult
     */
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JSONResult update(@RequestBody BcViewDTO bcViewDTO) {
        JSONResult jsonResult = null;
        try {
            long count = bcViewService.updateBcView(bcViewDTO);
            if (count > 0) {
                jsonResult = JSONResult.success();
            } else {
                jsonResult = JSONResult.fail(201, "操作失败");
            }
        } catch (Exception e) {
            LOGGER.error("Class BcViewController:Method update:OutParam E: " + e.getMessage());
            jsonResult = JSONResult.fail(201, e.getMessage());
        }
        return jsonResult;
    }

    /**
     * @param bcViewDTO
     * @Description 删除记录
     * @date: 2019/6/27 14:43
     * @return: com.mes.cloud.util.JSONResult
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JSONResult delete(@RequestBody BcViewDTO bcViewDTO) {
        JSONResult jsonResult = null;
        try {
            long count = bcViewService.delBcViewById(bcViewDTO.getId());
            if (count > 0) {
                jsonResult = JSONResult.success();
            } else {
                jsonResult = JSONResult.fail(201, "操作失败");
            }
        } catch (Exception e) {
            LOGGER.error("Class BcViewController:Method delete:OutParam E: " + e.getMessage());
            jsonResult = JSONResult.fail(201, e.getMessage());
        }
        return jsonResult;
    }

    /**
     * @param bcViewDTO
     * @Description 获取指定记录
     * @date: 2019/6/27 14:44
     * @return: com.mes.cloud.util.JSONResult
     */
    @ApiOperation(value = "根据ID获取详情")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public JSONResult get(@RequestBody BcViewDTO bcViewDTO) {
        JSONResult jsonResult = null;
        try {
            BcViewDTO bcViewDTOR = bcViewService.getBcViewById(bcViewDTO.getId());
            jsonResult = JSONResult.success(bcViewDTOR);
        } catch (Exception e) {
            LOGGER.error("Class BcViewController:Method get :inParam e: " + e.getMessage());
            jsonResult = JSONResult.fail(201, e.getMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据code获取详情")
    @RequestMapping(value = "/getByCode", method = RequestMethod.POST)
    public JSONResult getByCode(@RequestBody BcViewDTO bcViewDTO) {
        JSONResult jsonResult = null;
        try {
            BcViewDTO bcViewDTOR = bcViewService.getByCode(bcViewDTO.getCode());
            jsonResult = JSONResult.success(bcViewDTOR);
        } catch (Exception e) {
            LOGGER.error("Class BcViewController:Method get :inParam e: " + e.getMessage());
            jsonResult = JSONResult.fail(201, e.getMessage());
        }
        return jsonResult;
    }

    /**
     * @param map
     * @Description 批量删除
     * @date: 2019/6/27 14:44
     * @return: com.mes.cloud.util.JSONResult
     */
    @ApiOperation(value = "删除 批量")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public JSONResult batchDelete(@RequestBody Map<Object, Object> map) {
        JSONResult jsonResult = null;
        try {
            bcViewService.batchDelete(map);
            jsonResult = JSONResult.success();
        } catch (Exception e) {
            LOGGER.error("Class BcViewController:Method batchDelete :inParam e: " + e.getMessage());
            jsonResult = JSONResult.fail(201, e.getMessage());
        }
        return jsonResult;
    }

    /**
     * @param map
     * @Description 禁用启用
     * @date: 2019/6/27 14:44
     * @return: com.mes.cloud.util.JSONResult
     */
    @ApiOperation(value = "启用禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public JSONResult enable(@RequestBody Map<Object, Object> map) {
        JSONResult jsonResult = null;
        try {
            long count = bcViewService.enable(map);
            if (count > 0) {
                jsonResult = JSONResult.success();
            } else {
                jsonResult = JSONResult.fail(201, "操作失败");
            }
        } catch (Exception e) {
            LOGGER.error("Class BcViewController:Method updateEnable:OutParam E: " + e.getMessage());
            jsonResult = JSONResult.fail(201, e.getMessage());
        }
        return jsonResult;
    }

    /**
     * 启用停用 批量
     * @param map
     * @return com.mes.cloud.util.JSONResult
     */
    @ApiOperation(value = "启用停用 批量")
    @RequestMapping(value = "/batchEnable", method = RequestMethod.POST)
    public JSONResult batchEnable(@RequestBody Map<Object, Object> map) {
        JSONResult result = new JSONResult();
        try {
            bcViewService.batchEnable(map);
            result = JSONResult.success();
        } catch (Exception e) {
            LOGGER.error(e.toString());
            result = JSONResult.fail(201, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "导出")
    @RequestMapping(value = "/exportFile", method = RequestMethod.GET)
    public JSONResult exportFile(@RequestParam Map<Object, Object> map, HttpServletResponse response) {
        JSONResult result;
        try {
            bcViewService.exportFile(map, response);
            result = JSONResult.success();
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            result = JSONResult.fail(201, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "导入")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public JSONResult importExcel(@RequestParam MultipartFile file) {
        JSONResult result;
        try {
            result = JSONResult.success(bcViewService.importExcel(file));
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            result = JSONResult.fail(201, e.getMessage());
        }
        return result;
    }
}
